package com.example.msgsendmail;

import android.os.AsyncTask;
import android.util.Log;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailSender {
    private static final String TAG = "EmailSender";

    public interface EmailCallback {
        void onResult(boolean success);
    }

    public static void sendEmail(String subject, String content, EmailCallback callback) {
        new SendEmailTask(callback).execute(subject, content);
    }

    private static class SendEmailTask extends AsyncTask<String, Void, Boolean> {
        private final EmailCallback callback;

        public SendEmailTask(EmailCallback callback) {
            this.callback = callback;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            try {
                String subject = params[0];
                String content = params[1];

                // 设置邮件属性
                Properties props = new Properties();
                props.put("mail.smtp.host", EmailConfig.SMTP_HOST);
                props.put("mail.smtp.port", EmailConfig.SMTP_PORT);
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.starttls.required", "true");
                props.put("mail.smtp.ssl.protocols", "TLSv1.2");
                props.put("mail.smtp.connectiontimeout", "10000");
                props.put("mail.smtp.timeout", "10000");

                // 创建认证器
                Authenticator auth = new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(EmailConfig.FROM_EMAIL, EmailConfig.PASSWORD);
                    }
                };
                Log.i(TAG, "1111111111111111111111111");
                // 创建会话
                Session session = Session.getInstance(props, auth);
                session.setDebug(true); // 设置为true可以看到详细的SMTP调试信息
                Log.i(TAG, "1111111111111111111111111");
                // 创建邮件消息
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(EmailConfig.FROM_EMAIL));
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(EmailConfig.TO_EMAIL));
                message.setSubject(subject, "UTF-8");
                message.setText(content, "UTF-8");
                Log.i(TAG, "1111111111111111111111111");
                // 发送邮件
                Transport.send(message);
                Log.d(TAG, "邮件发送成功");
                return true;

            } catch (MessagingException e) {
                Log.e(TAG, "邮件发送失败: " + e.getMessage(), e);
                return false;
            } catch (Exception e) {
                Log.e(TAG, "发送邮件时出现未知错误: " + e.getMessage(), e);
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean success) {
            if (callback != null) {
                callback.onResult(success);
            }
        }
    }
}